package com.example.qihuibao.utils

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.SoundPool
import com.example.qihuibao.R


/**
 * 本地提示音播放器
 * SoundPool用于播放声音短，文件小的音频，延时短
 * @author: heiyulong
 * @date: 2021/4/16
 */
object RxSoundTool {

    private const val MAX_SOUNDS = 2
    private var soundPool: SoundPool? = null
    private var soundID: Int? = null
    private var scanFaceFailId: Int? = null
    private var authFaceFailId: Int? = null
    private var streamId: Int? = null

    fun init(appContext: Context) {
        val builder = SoundPool.Builder()
        //设置最多容纳的流数
        builder.setMaxStreams(MAX_SOUNDS)
        val attrBuilder = AudioAttributes.Builder()
        attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC)
        attrBuilder.setUsage(AudioAttributes.USAGE_MEDIA)
        attrBuilder.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        builder.setAudioAttributes(attrBuilder.build())
        soundPool = builder.build()
        soundID = soundPool?.load(appContext, R.raw.allow_passage, 1)
        scanFaceFailId = soundPool?.load(appContext, R.raw.scan_face_fail, 1)
        authFaceFailId = soundPool?.load(appContext, R.raw.auth_face_fail, 1)
    }

    /**
     * 播放音频
     */
    fun playSuccess() {
        if (soundPool == null || soundID == null) {
            return
        }
//        soundPool?.setOnLoadCompleteListener { soundPool: SoundPool, _, _ ->
        streamId = soundPool?.play(
            soundID!!, 1f, 1f,  //右声道：0.0f ~ 1.0f
            1,  //播放优先级：0表示最低优先级
            0, 1f
        ) //播放速度：1是正常，范围从0~2
//        }
    }

    /**
     * 人脸识别失败
     */
    fun playScanFail() {
        if (soundPool == null || soundID == null) {
            return
        }
//        soundPool?.setOnLoadCompleteListener { soundPool: SoundPool, _, _ ->
        streamId = soundPool?.play(
            scanFaceFailId!!, 1f, 1f,  //右声道：0.0f ~ 1.0f
            1,  //播放优先级：0表示最低优先级
            0, 1f
        ) //播放速度：1是正常，范围从0~2
//        }
    }

    /**
     * 人脸验证失败
     */
    fun playAuthFail() {
        if (soundPool == null || soundID == null) {
            return
        }
//        soundPool?.setOnLoadCompleteListener { soundPool: SoundPool, _, _ ->
        streamId = soundPool?.play(
            authFaceFailId!!, 1f, 1f,  //右声道：0.0f ~ 1.0f
            1,  //播放优先级：0表示最低优先级
            0, 1f
        ) //播放速度：1是正常，范围从0~2
//        }
    }

    /**
     * 暂停
     * @param resId
     */
    fun pause() {
        if (streamId != null) {
            soundPool?.pause(streamId!!)
        }
    }

    /**
     * 继续
     */
    fun resume() {
        if (streamId != null) {
            soundPool?.resume(streamId!!)
        }
    }

    /**
     * 停止
     */
    fun stop() {
        if (streamId != null) {
            soundPool?.stop(streamId!!)
        }
    }

    /**
     * 资源释放
     */
    fun release() {
        soundPool?.autoPause()
        soundPool?.release()
    }
}
